package com.hsgene.message.service.impl;

import com.hsgene.common.util.check.ObjectAttrUtils;
import com.hsgene.common.util.exception.ErrorCode;
import com.hsgene.common.util.mq.MQUtils;
import com.hsgene.message.config.MQProducer;
import com.hsgene.message.service.MqService;
import com.hsgene.mq.DcwBackInfo;
import com.hsgene.mq.DcwModel;
import com.hsgene.mq.YinZiOrderModel;
import com.hsgene.mq.constant.MqErrorCode;
import com.hsgene.order.exception.ObjectAttributeInfoEnum;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @author wxf
 * @date 2018/10/29 9:27
 **/
@Service
public class MqServiceImpl implements MqService {

    /**
     * Log
     */
    private static Logger LOGGER = LogManager.getLogger(MqServiceImpl.class);
    /**
     * 录入申请Tag
     */
    @Value("${tag.apply}")
    private String applyTag;
    /**
     * 补充材料Tag
     */
    @Value("${tag.addMaterials}")
    private String addMaterialsTag;
    /**
     * dcw编号Tag
     */
    @Value("${tag.dcwNo}")
    private String dcwNoTag;
    /**
     * 审核积分Tag
     */
    @Value("${tag.verifyIntegral}")
    private String verifyIntegralTag;
    /**
     * 积分发放完成更新状态Tag
     */
    @Value("${tag.grantIntegral}")
    private String grantIntegralTag;

    @Resource
    private MQProducer producer;

    /**
     * sendMQ
     *
     * @param content 消息内容
     * @return
     */
    @Override
    public ErrorCode sendMQ(Map<String, Object> content) {
        try {
            //校验参数
            return checkParamsAndSend(content);
        } catch (Exception e) {
            LOGGER.error("send message is error", e);
            return MqErrorCode.MQ_SEND_IS_EXCEPTION;
        }
    }

    private ErrorCode checkParamsAndSend(Map<String, Object> map) {
        String tagType = (String) map.get("tagType");
        map.remove(tagType);
        try {
            Object value = null;
            //申请录入
            if (applyTag.equals(tagType)) {
                YinZiOrderModel orderModel = new YinZiOrderModel();
                String result = ObjectAttrUtils.setFieldValue(map, orderModel, "totalCharges",
                        "totalCommission");
                if (StringUtils.isNotEmpty(result)) {
                    return ObjectAttributeInfoEnum.getEnum(result);
                }
                value = orderModel;
            }
            //补充录入,重新提交
            else if (addMaterialsTag.equals(tagType)) {
                DcwBackInfo dcwBackInfo = new DcwBackInfo();
                String result = ObjectAttrUtils.setFieldValue(map, dcwBackInfo, "remark", "type", "BACK",
                        "SUBMIT");
                if (StringUtils.isNotEmpty(result)) {
                    return ObjectAttributeInfoEnum.getEnum(result);
                }
                //重新提交
                dcwBackInfo.setType(DcwBackInfo.SUBMIT);
                value = dcwBackInfo;
            }
            //审核发放积分
            else if (verifyIntegralTag.equals(tagType)) {
                YinZiOrderModel order = new YinZiOrderModel();
                String result = ObjectAttrUtils.setFieldValue(map, order, null);
                if (StringUtils.isNotEmpty(result)) {
                    return ObjectAttributeInfoEnum.getEnum(result);
                }
                value = order;
            } else if (dcwNoTag.equals(tagType)) {
                DcwModel order = new DcwModel();
                BeanUtils.populate(order, map);
                value = order;
            }
            if (value == null) {
                return MqErrorCode.MQ_SEND_IS_ERROR;
            }
            MQUtils.send(value, producer.getProduct(), producer.getSendTopic(), tagType);
            return null;
        } catch (Exception e) {
            LOGGER.error("send message is error", e);
            return MqErrorCode.MQ_SEND_IS_EXCEPTION;
        }
    }

}
